﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DevExpress.XtraReports.UI;
using prjKQXS_DLL.Dal;
using prjKQXS_DLL;
using prjKQXS_DLL.Hop_dong;

namespace prjKQXS_Report.Hop_dong
{
    public partial class rptTongHopDaiLyCTy : DevExpress.XtraReports.UI.XtraReport
    {
        dsReports dsReports1 = new dsReports();

        int nam = 0;
        int thang = 0;
        int denthang = 0;

        public rptTongHopDaiLyCTy(int thang, int denThang, int nam)
        {
            InitializeComponent();

            this.nam = nam;
            this.thang = thang;
            this.denthang = denThang;

            if (thang == denthang)
            {
                lblNgaythang.Text = "Tháng " + thang + "  Năm " + nam;
            }
            else
            {
                lblNgaythang.Text = "Từ tháng " + thang + "  đến tháng " + denthang + "  năm " + nam;
            }
            lblDonvi.Text = "PHÒNG XSKT THÀNH PHỐ";
        }

        private void rptTongHopDaiLyCTy_BeforePrint(object sender, System.Drawing.Printing.PrintEventArgs e)
        {
            Report_DAL report_dal = new Report_DAL();
            dsReports1 = report_dal.reportDungChung(dsReports1);

            clsBienDongDL cls = new clsBienDongDL();
            cls.OnAction += new clsBienDongDL.OnActionHandler(cls_OnAction);
            cls.OnError += new clsBienDongDL.OnErrorHandler(cls_OnError);

            int namM = thang == 1 ? nam - 1 : nam;
            DateTime CuoiThangTruoc = clsCommon.maxNgay(namM, thang - 1 == 0 ? 12 : thang - 1);
            DataTable dt = cls.loadData_CT(thang, denthang, nam, CuoiThangTruoc);

            dsReports1.Tables.Remove("rptDSDaiLy_Cty");
            dsReports1.Tables.Add(dt);
            this.DataSource = dsReports1;
            this.DataMember = "rptDSDaiLy_Cty";
        }

        #region cls event --------------------------------------------
        private void cls_OnAction(SqlException e)
        {
            cls_OnError(e);
        }

        private void cls_OnError(Exception e)
        {
            XtraMessageBox.Show("Một lỗi chưa được xử lý đã xảy ra. Chi tiết lỗi: \n" + e.Message, "Exception", MessageBoxButtons.OK, MessageBoxIcon.Error);
            this.CloseRibbonPreview();
        }
        #endregion ---------------------------------------------------

        int stt = 0;
        private void Detail_BeforePrint(object sender, System.Drawing.Printing.PrintEventArgs e)
        {
            xrtcSTT.Text = (++stt).ToString();
        }
    }
}
